---------------------------------------分割线-----------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------
local ServerOHOH = _G.ServerOHOH;
local goods_cfg = ServerOHOH.config.Goods;
local InstanceManager = ServerOHOH.load.InstanceManager();

local Pid2Goods={}
local BUY_ARG={}
local function_list = ServerOHOH.function_list;
function_list.buy_goods = true;
local M = ServerOHOH.class.new(script, ServerOHOH.files.BaseComponent());
local Server_PayManager = ServerOHOH.load.Server_PayManager();

--------------------------
local Check = function(player, pid, gid)
    local arg=BUY_ARG[player.main_inst][gid]

	if arg then
		return   player:GetComponent(_G.CommonOHOH.load.Common_MallCtrl()):Check(gid,unpack(arg))
	else
		return   player:GetComponent(_G.CommonOHOH.load.Common_MallCtrl()):Check(gid)
	end

end

local Handle=function(p, pid, gid)
	-- local player=  InstanceManager:FindObject(playerInst);
	local player=p
    player:GetComponent(ServerOHOH.load.Server_ThePlayerRankComponent()):SaveRankIncrementData("sponsor",goods_cfg[gid].price)
    local arg=BUY_ARG[player.main_inst][gid]
	if arg then
		player:GetComponent(_G.CommonOHOH.load.Common_MallCtrl()):Buy(gid,unpack(arg))
	else
		player:GetComponent(_G.CommonOHOH.load.Common_MallCtrl()):Buy(gid)
	end
   
end
--------------------------
for id,v in pairs(goods_cfg) do
    Pid2Goods[v.pid]=v
    Server_PayManager:RegistProduct(v.pid, Check,Handle, id);
end


function M.new(self)
	return M(self);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self:InitHandleOnBuyPremium();
end;

function M.InitHandleOnBuyPremium(self)
	Server_PayManager:RegistHandleOnBuyPremium(self, self.OnBuyPremium);
end

function M.OnBuyPremium(self, player)
	
end


function M.dtor(self)
	M.super.dtor(self);
end;

function M.BuyGoods(self, player, goodId,...)
	assert(goodId, "param id is nil");
	assert(player, "param player is nil");
	local goods = goods_cfg[goodId];
	assert(goods, "no cfg for goods id = " .. goodId);

    BUY_ARG[player.main_inst][goodId]={...}

	if goods.isAsset then
		local isSuccess, data = Server_PayManager:BuyAsset(player, goods.pid);
        BUY_ARG[player.main_inst][goodId]=nil
		return isSuccess, data;
	else
		local isSuccess, data = Server_PayManager:BuyProduct(player, goods.pid);
        BUY_ARG[player.main_inst][goodId]=nil
		return isSuccess, data;
	end
end;

ServerOHOH.function_manager:SetHandle(function_list.buy_goods, function(player, goods_id,...)

	-- warn("===========>buy_goods:",arg)
	local Server_MallComponent = _G.server:GetComponent(ServerOHOH.load.Server_MallComponent());
	if not Server_MallComponent then
		return false;
	end;
	return Server_MallComponent:BuyGoods(player, goods_id,...);
	
end);

----------------------------GamePass购买回调-------------------
local InstanceManager = ServerOHOH.load.InstanceManager();
local MarketplaceService = game:GetService("MarketplaceService")
local function onPromptPurchaseFinished(player, pid, purchaseSuccess)
	if purchaseSuccess  then
		local playerObj = InstanceManager:FindObject(player);
		local cfg=Pid2Goods[pid]
		assert(playerObj,"playerObj==nil")
		playerObj:GetComponent(ServerOHOH.load.Server_ThePlayerRankComponent()):SaveRankIncrementData("sponsor",cfg.price)
        -- player.RobuxBuyEvent:Acton(cfg.id)
		print("____onPromptPurchaseFinished_______GamePass___________")
        Handle(playerObj,pid,cfg.id)
	end
end

-- Connect "PromptGamePassPurchaseFinished" events to the function
MarketplaceService.PromptGamePassPurchaseFinished:Connect(onPromptPurchaseFinished)

-- for _,v in pairs(script:GetChildren()) do
--     local buyCtrl=require(v)
--     M.RegistProductHandle(v.Name,buyCtrl.Check,buyCtrl.Handle)
-- end


game.Players.PlayerAdded:Connect(function(player)
    BUY_ARG[player]={}
end)
return M;
